//
//  CAUpgradeViewController.m
//  LNH
//
//  Created by twentylx on 2018/2/8.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "CAUpgradeViewController.h"
#import "CAUpgradeTableViewCell.h"
#import "CALevelApi.h"
#import "CAUpgradeGoodsViewController.h"
#import "CASelectDistrictViewController.h"

@interface CAUpgradeViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *upgradeTableView;
@property (nonatomic, strong) NSArray *upgradeArray;

@end

static NSString *cellIdentifier = @"UpdateGradeCell";

@implementation CAUpgradeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/// 初始化数据
- (void)lm_initialDefaultsForController {
    [self loadData];
}

/// 绑定 vm
- (void)lm_bindViewModelForController {
}

/// 创建视图
- (void)lm_createViewForController {
    [self.view setNeedsUpdateConstraints];
    
    [self.view addSubview:self.upgradeTableView];
}

/// 配置导航栏
- (void)lm_configNavigationForController {
    self.navigationItem.title = @"选择代理商级别";
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

#pragma mark layout
- (void)updateViewConstraints {
    [super updateViewConstraints];
    
    [_upgradeTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

#pragma mark get & set
- (UITableView *)upgradeTableView {
    if (!_upgradeTableView) {
        _upgradeTableView = [UITableView new];
        _upgradeTableView.dataSource = self;
        _upgradeTableView.delegate = self;
        _upgradeTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_upgradeTableView registerClass:[CAUpgradeTableViewCell class] forCellReuseIdentifier:cellIdentifier];
    }
    return _upgradeTableView;
}

#pragma mark private method
- (void) loadData {
    CALevelApi *levelApi = [[CALevelApi alloc] init];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [levelApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        CALevelApi *api = (CALevelApi *) request;
        if (api.done) {
            _upgradeArray = api.responseObject[@"result"];
            [_upgradeTableView reloadData];
        } else {
            [SVProgressHUD lm_displayErrorWithStatus:api.errorMsg];
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [SVProgressHUD lm_displayErrorWithStatus:@"请求出错"];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.upgradeArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CAUpgradeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    NSDictionary *dict = _upgradeArray[indexPath.row];
    cell.nameLabel.text = dict[@"level_name"];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //
    return cell;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 200;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = self.upgradeArray[indexPath.row];
    if ([dict[@"id"] integerValue] > 13) {
        CASelectDistrictViewController *selectDisctictVC = [CASelectDistrictViewController new];
        selectDisctictVC.levelId = [NSString stringWithFormat:@"%ld", [dict[@"id"] integerValue]];
        [self.navigationController pushViewController:selectDisctictVC animated:YES];
    } else {
        CAUpgradeGoodsViewController *goodsVC = [CAUpgradeGoodsViewController new];
        goodsVC.level = [NSString stringWithFormat:@"%ld", [dict[@"id"] integerValue]];
        [self.navigationController pushViewController:goodsVC animated:YES];
    }
}

@end
